package com.android.powerbankpad.network;


import com.android.powerbankpad.config.Constants;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;
import com.ws.universal.tools.OrrorLog;
import com.ws.universal.tools.utils.CacheUtils;
import com.ws.universal.tools.utils.LogcatUtils;
import com.ws.universal.tools.utils.NetWorkUtils;
import com.ws.universal.tools.utils.StringUtils;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * @author:ws
 * @date: 2021/8/9 18:31
 * @desc 使用示例
 */
public class okHttpModel {
    private static HttpHeaders headers = new HttpHeaders();
    private String locat = "";

    //添加 header
    public static HttpHeaders getHeaders() {
        return headers;
    }

    public static void setHeaders(HttpHeaders headers) {
        okHttpModel.headers = headers;
    }

    public static void get(String url, Map<String, String> map, final int ids, final NetWorkListener listener) {
        LogcatUtils.e("get url " + url);
        OkGo.<String>get(StringUtils.getString(url, "?lang=", CacheUtils.getString(Constants.langCode))).params(map).headers(getHeaders()).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (listener != null && response != null) {
                    if (response != null && !StringUtils.isEmpty(response.body())) {
                        String body = response.body().replaceAll("http:", "https:");
                        //LogcatUtils.e(" body get " + JSONFormat.format(body));
                        try {
                            JSONObject object = new JSONObject(body);
                            int code = object.optInt("status");
                            String msg = object.optString("msg");
                            CommonalityModel commonality = new CommonalityModel();
                            commonality.setStatusCode(code);
                            commonality.setMsg(msg);
                            if (commonality.getStatusCode() == 1) {
                                listener.onSucceed(object, ids, commonality);//请求成功后通过接口返回数据
                            } else {
                                ToastUtils.showShort(commonality.getMsg());
                                LogcatUtils.e(" onFail   Url " + url + commonality.getMsg());
                                OrrorLog.updateLog(" onFail   Url " + url + "\n:" + object);
                                listener.onFail(object, ids, commonality);//请求成功后通过接口返回数据
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastUtils.showShort("onError JSON解析错误 Url " + url + "\n:" + e.getMessage(), 10000);
                            LogcatUtils.e("onError JSON解析错误 Url " + url + e.getMessage());
                            OrrorLog.updateLog(" JSON解析错误   Url " + url + "\n:" + e.getMessage());
                            listener.onError(ids, e);
                        }
                    }
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                ToastUtils.showShort("onError service错误 Url " + url + "\n:" + response.getException().toString(), 10000);
                LogcatUtils.e("onError service错误 Url " + url + response.getException().toString());
                OrrorLog.updateLog(" service错误   Url " + url + "\n:" + response.getException().toString());
                if (listener != null && response != null && response.getException() != null) {
                    listener.onError(ids, (Exception) response.getException());

                }

            }
        });
    }


    public static void post(String url, Map<String, String> map, final int ids, final NetWorkListener listener) {

        LogcatUtils.e("post url " + url);
        OkGo.<String>post(StringUtils.getString(url, "?lang=", CacheUtils.getString(Constants.langCode))).params(map).headers(getHeaders()).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (listener != null && response != null) {
                    if (response != null && !StringUtils.isEmpty(response.body())) {
                        String body = response.body().replaceAll("http:", "https:");
                        //LogcatUtils.e(" body post " + JSONFormat.format(body));
                        try {
                            JSONObject object = new JSONObject(body);
                            int code = object.optInt("status");
                            String msg = object.optString("msg");
                            CommonalityModel commonality = new CommonalityModel();
                            commonality.setStatusCode(code);
                            commonality.setMsg(msg);
                            if (commonality.getStatusCode() == 1) {
                                listener.onSucceed(object, ids, commonality);//请求成功后通过接口返回数据
                            } else {
                                ToastUtils.showShort(commonality.getMsg());
                                LogcatUtils.e(" onFail   Url " + url + commonality.getMsg());
                                OrrorLog.updateLog(" onFail   Url " + url + "\n:" + object);
                                listener.onFail(object, ids, commonality);//请求成功后通过接口返回数据
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastUtils.showShort("onError JSON解析错误 Url " + url + "\n:" + e.getMessage(), 10000);
                            LogcatUtils.e("onError JSON解析错误 Url " + url + e.getMessage());
                            OrrorLog.updateLog(" JSON解析错误   Url " + url + "\n:" + e.getMessage());
                            listener.onError(ids, e);
                        }
                    }
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                ToastUtils.showShort("onError service错误 Url " + url + "\n:" + response.getException().toString(), 10000);
                LogcatUtils.e("onError service错误 Url " + url + response.getException().toString());
                OrrorLog.updateLog(" service错误   Url " + url + "\n:" + response.getException().toString());
                if (listener != null && response != null && response.getException() != null) {
                    listener.onError(ids, (Exception) response.getException());

                }
            }
        });
    }


    public static void get(String url, Map<String, String> map, final int ids, HttpHeaders headers, final NetWorkListener listener) {
        LogcatUtils.e("get url " + url);
        OkGo.<String>get(url).params(map).headers(headers).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (listener != null && response != null) {
                    if (response != null && !StringUtils.isEmpty(response.body())) {
                        String body = response.body().replaceAll("http:", "https:");
                        //LogcatUtils.e(" body get " + JSONFormat.format(body));
                        try {
                            JSONObject object = new JSONObject(body);
                            int code = object.optInt("status");
                            String msg = object.optString("msg");
                            CommonalityModel commonality = new CommonalityModel();
                            commonality.setStatusCode(code);
                            commonality.setMsg(msg);
                            if (commonality.getStatusCode() == 1) {
                                listener.onSucceed(object, ids, commonality);//请求成功后通过接口返回数据
                            } else {
                                ToastUtils.showShort(commonality.getMsg());
                                LogcatUtils.e(" onFail   Url " + url + commonality.getMsg());
                                OrrorLog.updateLog(" onFail   Url " + url + "\n:" + object);
                                listener.onFail(object, ids, commonality);//请求成功后通过接口返回数据
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastUtils.showShort("onError JSON解析错误 Url " + url + "\n:" + e.getMessage(), 10000);
                            LogcatUtils.e("onError JSON解析错误 Url " + url + e.getMessage());
                            OrrorLog.updateLog(" JSON解析错误   Url " + url + "\n:" + e.getMessage());
                            listener.onError(ids, e);
                        }
                    }
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                ToastUtils.showShort("onError service错误 Url " + url + "\n:" + response.getException().toString(), 10000);
                LogcatUtils.e("onError service错误 Url " + url + response.getException().toString());
                OrrorLog.updateLog(" service错误   Url " + url + "\n:" + response.getException().toString());
                if (listener != null && response != null && response.getException() != null) {
                    listener.onError(ids, (Exception) response.getException());

                }

            }
        });
    }


}
